package algorithm.middle;

public class SolveEquation640 {

    public String solveEquation(String equation) {
        //拆分等号左右两边
        String[] split = equation.split("=");
        if (split[0].charAt(0)!='-'){
            split[0] = '+' + split[0];
        }
        if (split[1].charAt(0)!='-'){
            split[1] = '+' + split[1];
        }
        //合并同类项。x在左，数在右
        int left_x = 0;
        int right = 0;
        //左表达式
        char[] lchars = split[0].toCharArray();
        for (int i = 0; i < lchars.length; i++) {
            if (lchars[i]=='+'){
                i++;
                //符号后面是x，直接给系数+1
                if (lchars[i]=='x'){
                    left_x++;
                    continue;
                }
                //处理数字和符号的情况
                int count = 0;
                //如果是数字
                while (lchars[i] >= '0' && lchars[i] <= '9') {
                    count = count * 10 + lchars[i] - '0';
                    i++;
                    if (i==lchars.length){
                        break;
                    }
                }
                if (i<lchars.length && lchars[i]=='x'){
                    left_x += count;
                }else {
                    right -= count;
                    i--;
                }
            } else if (lchars[i]=='-'){
                i++;
                //符号后面是x，直接给系数+1
                if (lchars[i]=='x'){
                    left_x--;
                    continue;
                }
                //处理数字和符号的情况
                int count = 0;
                //如果是数字
                while (lchars[i] >= '0' && lchars[i] <= '9') {
                    count = count * 10 + lchars[i] - '0';
                    i++;
                    if (i==lchars.length){
                        break;
                    }
                }
                if (i<lchars.length && lchars[i]=='x'){
                    left_x -= count;
                }else {
                    right += count;
                    i--;
                }
            }
        }
        //右表达式
        char[] rchars = split[1].toCharArray();
        for (int i = 0; i < rchars.length; i++) {
            if (rchars[i]=='-'){
                i++;
                //符号后面是x，直接给系数+1
                if (rchars[i]=='x'){
                    left_x++;
                    continue;
                }
                //处理数字和符号的情况
                int count = 0;
                //如果是数字
                while (rchars[i] >= '0' && rchars[i] <= '9') {
                    count = count * 10 + rchars[i] - '0';
                    i++;
                    if (i==rchars.length){
                        break;
                    }
                }
                if (i<rchars.length && rchars[i]=='x'){
                    left_x += count;
                }else {
                    right -= count;
                    i--;
                }
            } else if (rchars[i]=='+'){
                i++;
                //符号后面是x，直接给系数+1
                if (rchars[i]=='x'){
                    left_x--;
                    continue;
                }
                //处理数字和符号的情况
                int count = 0;
                //如果是数字
                while (rchars[i] >= '0' && rchars[i] <= '9') {
                    count = count * 10 + rchars[i] - '0';
                    i++;
                    if (i==rchars.length){
                        break;
                    }
                }
                if (i<rchars.length && rchars[i]=='x'){
                    left_x -= count;
                }else {
                    right += count;
                    i--;
                }
            }
        }
        // 0x = n x无解
        if (left_x == 0 && right != 0) {
            return "No solution";
        }
        // 0x = 0 x无限解
        if (left_x == 0 && right == 0) {
            return "Infinite solutions";
        }
        if (right%left_x==0){
            right = right / left_x;
            left_x = 1;
        }
        if (left_x == 1) {
            return "x=" + right;
        }
        return left_x + "x=" + right;

    }


    public static void main(String[] args) {
        System.out.println(2 / -2);
        new SolveEquation640().solveEquation("2x+3x-6x=x+2");
    }
}
